package com.qqtech.qquan.push.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.push.dao.PushAudienceDao;
import com.qqtech.qquan.push.model.PushAudience;

@Service
public class PushAudienceServiceImpl extends BaseServiceImpl<PushAudience> implements PushAudienceService {
	public static final Logger log = LoggerFactory.getLogger(PushAudienceServiceImpl.class);

	@Resource
	private PushAudienceDao pushAudienceDao;
	@Resource
	private PushJgService pushJgService;

	@Override
	protected BaseDao<PushAudience> getBaseDao() {
		return pushAudienceDao;
	}

	@Override
	public List<String> getRegistrationIdsByUserIds(List<Integer> userIds) {
		if (userIds == null || userIds.size() == 0) {
			return null;
		}
		List<String> list = new ArrayList<String>(userIds.size());
		for (Integer userId : userIds) {
			String regId = this.getRegistrationIdByUserId(userId);
			if (StringUtil.isNotBlank(regId) && !"null".equalsIgnoreCase(regId)) {
				list.add(regId);
			}
		}
		return list;
	}

	@Override
	public String getRegistrationIdByUserId(Integer userId) {
		if (userId == null || userId.intValue() == 0) {
			return null;
		}
		PushAudience pushAudience = new PushAudience();
		pushAudience.setUserId(userId);
		List<PushAudience> lstPushAudience = pushAudienceDao.queryList(pushAudience,
				new PageKit(new PageOrder(Direction.DESC, "updAt")));
		if (lstPushAudience != null && lstPushAudience.size() > 0) {
			pushAudience = lstPushAudience.get(0);
			return pushAudience.getRegistrationId();
		}
		return null;
	}

	@Override
	public void addNewAudience(String registrationId, Integer userId) {
		if (StringUtil.isBlank(registrationId) || userId == null || userId.intValue() == 0) {
			return;
		}
		PushAudience pushAudience = new PushAudience();
		pushAudience.setRegistrationId(registrationId);
		pushAudience.setUserId(userId);
		PushAudience exist = pushAudienceDao.getOne(pushAudience);
		if (exist == null) {
			pushAudience.setTag("");
			pushAudience.setAlias("");
			pushAudienceDao.insert(pushAudience);
		} else {
			pushAudience = new PushAudience();
			pushAudience.setId(exist.getId());
			pushAudience.setUpdAt(TimeUtil.now());
			pushAudienceDao.updateById(pushAudience);
		}
	}

	@Override
	public void updateTagAlias(Integer userId, Integer newCommunity, Integer oldCommunity) {
		if (userId == null || userId.intValue() == 0) {
			return;
		}
		Set<String> tagsToAdd = new HashSet<String>();
		if (newCommunity != null && newCommunity.intValue() != 0) {
			tagsToAdd.add("" + newCommunity);
		}
		Set<String> tagsToRemove = new HashSet<String>();
		if (oldCommunity != null && oldCommunity.intValue() != 0) {
			tagsToRemove.add("" + oldCommunity);
		}
		this.updateTagAlias(userId, "" + userId, tagsToAdd, tagsToRemove);
	}

	@Override
	public void updateTagAlias(Integer userId, String alias, Set<String> tagsToAdd, Set<String> tagsToRemove) {
		if (userId == null || userId.intValue() == 0) {
			return;
		}
		String regId = this.getRegistrationIdByUserId(userId);
		if (StringUtil.isBlank(regId) || "null".equalsIgnoreCase(regId)) {
			return;
		}
		PushAudience pushAudience = new PushAudience();
		pushAudience.setRegistrationId(regId);
		pushAudience.setUserId(userId);
		PushAudience exist = pushAudienceDao.getOne(pushAudience);
		if (exist == null) {
			return;
		}
		if (tagsToAdd == null) {
			tagsToAdd = new HashSet<String>();
		}
		String oldTags = exist.getTag();
		if (StringUtil.isNotBlank(oldTags)) {
			String[] oldTagArr = oldTags.split(";");
			for (String string : oldTagArr) {
				tagsToAdd.add(string);
			}
		}
		if (tagsToRemove != null && tagsToRemove.size() > 0) {
			for (String string : tagsToRemove) {
				tagsToAdd.remove(string);
			}
		}
		StringBuffer newTagSb = new StringBuffer();
		if (tagsToAdd != null && tagsToAdd.size() > 0) {
			for (String string : tagsToAdd) {
				if (StringUtil.isNotBlank(newTagSb.toString())) {
					newTagSb.append(";");
				}
				newTagSb.append(string);
			}
		}
		String newTag = newTagSb.toString();
		if (StringUtil.isBlank(newTag) && StringUtil.isBlank(alias)) {
			return;
		}
		pushAudience = new PushAudience();
		pushAudience.setId(exist.getId());
		if (StringUtil.isNotBlank(newTag)) {
			pushAudience.setTag(newTag);
		}
		if (StringUtil.isNotBlank(alias)) {
			pushAudience.setAlias(alias);
		}
		pushAudience.setUpdAt(TimeUtil.now());
		pushAudienceDao.updateById(pushAudience);
		pushJgService.updateDeviceTagAlias(regId, alias, tagsToAdd, tagsToRemove);
	}

	public static void main(String[] args) {
		String[] ss = "12;34;5678;78;89".split(";");
		Set<String> tagsToAdd = new HashSet<String>();
		for (String string : ss) {
			tagsToAdd.add(string);
		}
		/*
		 * tagsToAdd.add("12"); tagsToAdd.add("34"); tagsToAdd.add("5678");
		 * tagsToAdd.add("78"); tagsToAdd.add("89");
		 */
		Set<String> tagsToMove = new HashSet<String>();
		tagsToMove.add("34");
		tagsToMove.add("45");
		tagsToMove.add("67");
		for (String string : tagsToMove) {
			tagsToAdd.remove(string);
		}
		System.out.println(tagsToAdd.toString());
	}
}